<?php
/**
 * Created by PhpStorm.
 * User: KaZeline
 * Date: 15-9-29
 * Time: 下午3:35
 */

namespace Api;

class Qunar_2B{
    //配置信息
    public $version='1.1.0';
    public $qunar_URL = '/openapi/';
    public $partnerCode = 1835071063;


    public function share_array(array $request_array){
        ksort($request_array);
        $request_array = array(
            'version'=>$this->version,
            'partnerCode'=>$this->partnerCode,
        );
        return $request_array;
    }

    //2.1产品列表
    public function productInfoList(int $pageNo=1,int $perPageSize=20){
        if($perPageSize>500) return false;
        $action = 'productInfoList';
        $request_array = array(
            'pageNo'=>$pageNo,
            'perPageSize'=>$perPageSize,
        );
        return qunar_post($action,$request_array);
    }
    //2.2单个产品信息
    public function singleProduct(int $productId){
        $action = 'singleProduct';
        $request_array = array(
            'productId'=>$productId,
        );
        return qunar_post($action,$request_array);
    }

    //2.3产品报价信息
    public function priceInfoList(int $pageNo=1,int $perPageSize=20){
        if($perPageSize>500) return false;
        $action = 'priceInfoList';
        $request_array = array(
            'pageNo'=>$pageNo,
            'perPageSize'=>$perPageSize,
        );
        return qunar_post($action,$request_array);
    }

    //2.4单个产品报价
    public function singlePrice(int $productId){
        $action = 'singlePrice';
        $request_array = array(
            'productId'=>$productId,
        );
        return qunar_post($action,$request_array);
    }

    //2.5批量获取景点信息
    public function sightInfos(int $pageNo=1,int $perPageSize=20){
        $action = 'sightInfos';
        $request_array = array(
            'pageNo'=>$pageNo,
            'perPageSize'=>$perPageSize,
        );
        return qunar_post($action,$request_array);
    }

    //2.6按照景点Id获取景点信息 景点Id，多个景点Id以逗号分隔，限制50个以内
    public function getSightInfoBySightIds(string $sightIds){
        $action = 'getSightInfoBySightIds';
        $request_array = array(
            'sightIds'=>$sightIds,
        );
        return qunar_post($action,$request_array);
    }

    //2.7根据景点Id获取产品信息
    public function getProductInfoBySightIds(string $sightIds){
        $action = 'getProductInfoBySightIds';
        $request_array = array(
            'sightIds'=>$sightIds,
        );
        return qunar_post($action,$request_array);
    }

    /*
     * 提交查询
     * 加上固定的版本号和分销商号
     * 先把所有参数按键名排序 用&链接 然后md5 生成一个新的参数sign
     */
    public function qunar_post(string $action,array $request_array){

        $request_array['version'] = $this->version;
        $request_array['partnerCode'] = $this->partnerCode;
        ksort($request_array);
        $request_array['sign']  = md5(http_build_query($request_array));
        $request_data = http_build_query($request_array);
        $url = $this->qunar_URL.$action;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $request_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        $result = curl_exec($ch);
//        $result = simplexml_load_string( $result, 'SimpleXMLElement', LIBXML_NOCDATA );
        return $result;
    }
}

$qunar = new Qunar_2B();
$land = $qunar->productInfoList();
echo '<pre>';
print_r($land);
echo '</pre>';